home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / rename.c < prev    next >
C/C++ Source or Header  |  1996-10-30  |  3KB  |  112 lines

  1. /* rename.c -- BSD compatible directory function for System V
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #ifndef HAVE_RENAME
  23.  
  24. #ifdef HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #ifndef errno
  30. extern int errno;
  31. #endif
  32.  
  33. #ifdef STAT_MACROS_BROKEN
  34. #undef S_ISDIR
  35. #endif /* STAT_MACROS_BROKEN.  */
  36.  
  37. #if !defined(S_ISDIR) && defined(S_IFDIR)
  38. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  39. #endif
  40.  
  41. #include "safe-stat.h"
  42.  
  43. /* Rename file FROM to file TO.
  44.    Return 0 if successful, -1 if not. */
  45.  
  46. int
  47. rename (from, to)
  48.      char *from;
  49.      char *to;
  50. {
  51.   struct stat from_stats, to_stats;
  52.   int pid, status;
  53.  
  54.   if (SAFE_STAT (from, &from_stats))
  55.     return -1;
  56.  
  57.   /* Be careful not to unlink `from' if it happens to be equal to `to' or
  58.      (on filesystems that silently truncate filenames after 14 characters)
  59.      if `from' and `to' share the significant characters. */
  60.   if (SAFE_STAT (to, &to_stats))
  61.     {
  62.       if (errno != ENOENT)
  63.         return -1;
  64.     }
  65.   else
  66.     {
  67.       if ((from_stats.st_dev == to_stats.st_dev)
  68.           && (from_stats.st_ino == to_stats.st_dev))
  69.         /* `from' and `to' designate the same file on that filesystem. */
  70.         return 0;
  71.  
  72.       if (unlink (to) && errno != ENOENT)
  73.         return -1;
  74.     }
  75.  
  76.   if (S_ISDIR (from_stats.st_mode))
  77.     {
  78.       /* Need a setuid root process to link and unlink directories. */
  79.       pid = fork ();
  80.       switch (pid)
  81.     {
  82.     case -1:        /* Error. */
  83.       error (1, errno, "cannot fork");
  84.  
  85.     case 0:            /* Child. */
  86.       execl (MVDIR, "mvdir", from, to, (char *) 0);
  87.       error (255, errno, "cannot run `%s'", MVDIR);
  88.  
  89.     default:        /* Parent. */
  90.       while (wait (&status) != pid)
  91.         /* Do nothing. */ ;
  92.  
  93.       errno = 0;        /* mvdir printed the system error message. */
  94.       if (status)
  95.         return -1;
  96.     }
  97.     }
  98.   else
  99.     {
  100.       if (link (from, to))
  101.     return -1;
  102.       if (unlink (from) && errno != ENOENT)
  103.     {
  104.       unlink (to);
  105.       return -1;
  106.     }
  107.     }
  108.   return 0;
  109. }
  110.  
  111. #endif
  112.